package com.lcx.movie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lcx.movie.common.Rep;
import com.lcx.movie.entity.ScheduleSeat;
import com.lcx.movie.entity.Seats;
import com.lcx.movie.service.ScheduleSeatService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 李丽君
 * @version 1.0
 * @Description 场次座位控制类
 * @date 2025/2/20 14:51
 */
@CrossOrigin(origins = "*")
@RestController
public class ScheduleSeatController {
    @Autowired
    private ScheduleSeatService scheduleSeatService;

    /**
     * 新增场次时同时新增场次座位表
     * @param map map对象
     * @return rep对象
     */
    @PostMapping("/saveScheduleSeats")
    public Rep saveScheduleSeats(@RequestBody Map<String, Object> map) {
        int hallId=map.get("hallId")==null?0:Integer.parseInt(map.get("hallId").toString());//影厅id
        // 在新增前先清除查到的数据
        QueryWrapper<ScheduleSeat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hall_id",hallId);
        boolean remove = this.scheduleSeatService.remove(queryWrapper);
        System.out.println("是否清除成功:"+remove);
        List<ScheduleSeat> scheduleSeatList = (List<ScheduleSeat>) map.get("scheduleSeatList");//影厅的座位集合数据
        // 批量新增座位
        this.scheduleSeatService.saveBatch(scheduleSeatList);
        System.out.println(scheduleSeatList);
        return new Rep();
    }
//    /**
//     * 检查当前影厅是否有座位信息
//     * @param hallId 影厅id
//     * @return 受影响的行数
//     */
//    @GetMapping("/getSeatsByHallId")
//    public String getSeatsByHallId(@Param("hallId") int hallId){
//        int seatsByHallId = this.scheduleSeatService.getSeatsByHallId(hallId);
//        System.out.println("seatsByHallId = " + seatsByHallId);
//        return seatsByHallId>0?"有座位":"无座位";
//    }

    /**
     * 根据场次id查询所有的座位
     * @param scheduleId 场次id
     * @return 座位集合
     */
    @GetMapping("/scheduleByScheduleId")
    public Rep scheduleByScheduleId(Integer scheduleId){
        // 调用service方法，根据scheduleId查询座位列表
        List<ScheduleSeat> seatsList = scheduleSeatService.scheduleByScheduleId(scheduleId);
        // 创建Rep对象，并添加座位列表数据
        return new Rep().addData("seatsList", seatsList);
    }

}
